import configparser
from src.Core.pet_assistant import PetAssistant
from src.Data.knowledge_graph import KnowledgeGraph
from src.Logging.log_manager import LogManager

# 读取数据库配置
config = configparser.ConfigParser()
config.read('config/db_config.ini')
db_config = {
    'host': config.get('DATABASE', 'host'),
    'port': config.getint('DATABASE', 'port'),
    'user': config.get('DATABASE', 'user'),
    'password': config.get('DATABASE', 'password'),
    'database': config.get('DATABASE', 'database'),
    'dialogue_table': config.get('DATABASE', 'dialogue_table')
}

class PetQAService:
    def __init__(self):
        self.logger = LogManager()
        self.assistant = PetAssistant()
        self.knowledge_graph = KnowledgeGraph(db_config)

    async def answer(self, question: str) -> str:
        """回答用户问题"""
        # 知识图谱优先
        answer = self.knowledge_graph.get_answer(question)
        if answer:
            self.logger.info(f"从知识图谱获取答案：{answer}")
            return answer
        # 智能问答
        self.logger.info("从智能助理获取答案")
        return await self.assistant.respond(question)

    def get_answer(self, question: str) -> str:
        """获取答案（兼容旧接口）"""
        return {"answer": question}
